const timestampInMillisToDateTime = (timestamp, timezone = 'Asia/Beijing') => {
  const date = new Date(timestamp);
  return date.toLocaleString('cmn', {timezone});
};

const timestampInSecondsToDateTime = (timestamp, timezone = 'Asia/Beijing') => {
  return timestampInMillisToDateTime(timestamp * 1000, timezone);
};

const durationSecToFormat = (seconds) => {
  let format = '';
  const second = 1, minute = second * 60, hour = minute * 60, day = hour * 24;
  const numDay = Math.floor(seconds / day);
  if (numDay > 0) {
    format += `${numDay}天`;
    seconds -= numDay * day;
  }
  const numHour = Math.floor(seconds / hour);
  if (numHour > 0 || format.length) {
    format += `${numHour}时`;
    seconds -= numHour * hour;
  }
  const numMinute = Math.floor(seconds / minute);
  if (numMinute > 0 || format.length) {
    format += `${numMinute}分`;
    seconds -= numMinute * minute;
  }
  return `${format}${seconds}秒`;
};

const timezoneMapper = {
  ID: {
    timezone: 'Asia/Jakarta',
    GMT: 'GMT+7'
  },
  SG: {
    timezone: 'Asia/Singapore',
    GMT: 'GMT+8'
  },
  PH: {
    timezone: 'Asia/Manila',
    GMT: 'GMT+8'
  },
  MY: {
    timezone: 'Asia/Kuala_Lumpur',
    GMT: 'GMT+8'
  },
  VN: {
    timezone: 'Asia/Ho_Chi_Minh',
    GMT: 'GMT+7'
  },
  TH: {
    timezone: 'Asia/Bangkok',
    GMT: 'GMT+7'
  }
};

export default {
  timestampInMillisToDateTime,
  timestampInSecondsToDateTime,
  durationSecToFormat,
  timezoneMapper
}
